package net.kingborn.erp.wc.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.kingborn.core.controller.BaseController;
import net.kingborn.core.controller.Controller;
import net.kingborn.core.entry.Result;
import net.kingborn.erp.wc.command.AttendanceQuery;
import net.kingborn.erp.wc.command.AttendanceSave;
import net.kingborn.erp.wc.model.Attendance;
import net.kingborn.erp.wc.service.AttendanceService;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.*;

@Controller("/attendance")
public class AttendanceController extends BaseController {

    @Autowired
    AttendanceService attendanceService;

    @PostMapping("/page")
    public Result page() {
        return doAction(AttendanceQuery.class);
    }

    @PostMapping("/saveInfo")
    public Result saveInfo() {
        return doAction(AttendanceSave.class);

    }

    @PostMapping("/download")
    public void download(HttpServletResponse response) {
        JSONObject map = new JSONObject();
        map.put("name", null);
        map.put("startDate", null);
        map.put("endDate", null);
        Page<Attendance> temp = attendanceService.pageSearch(1, 100, map);
        Page<Attendance> page = attendanceService.pageSearch(1, temp.getTotal(), map);
        List<Attendance> list = page.getRecords();
        HashMap<String, Attendance> nameGroup = new HashMap();

        for (Attendance attendance : list) {
            double hoursCount = 0;
            double addHoursCount = 0;
            double addPriceCount = 0;
            double allPriceCount = 0;
            double eatCount = 0;
            for (Attendance a : list) {
                if (a.getName().equals(attendance.getName())) {

                    if (!StringUtils.isEmpty(a.getHours())) {
                        hoursCount += Double.valueOf(a.getHours());
                    }
                    if (!StringUtils.isEmpty(a.getAddHours())) {
                        addHoursCount += Double.valueOf(a.getAddHours());
                    }
                    if (!StringUtils.isEmpty(a.getAddPrice())) {
                        addPriceCount += Double.valueOf(a.getAddPrice());
                    }
                    if (!StringUtils.isEmpty(a.getAllPrice())) {
                        allPriceCount += Double.valueOf(a.getAllPrice());
                    }
                    if (!StringUtils.isEmpty(a.getEatPrice())) {
                        eatCount += Double.valueOf(a.getEatPrice());
                    }

                }
            }
            hoursCount = hoursCount / 8.5;
            Attendance at = new Attendance();
            at.setName(attendance.getName());//姓名
            at.setDate(attendance.getSex());//性别
            at.setSex(format2(hoursCount));//工时合计
            at.setHours(format2(addHoursCount));//加班合计
            at.setAddHours(format2(addPriceCount));//加班工资合计
            at.setAddPrice(format2(allPriceCount));//总工资合计
            at.setAllPrice(format2(eatCount));//预支合计
            at.setEatPrice(format2(allPriceCount - eatCount));//剩余未结
            nameGroup.put(attendance.getName(), at);
        }

        Attendance all = new Attendance();
        list.add(all);
        list.add(all);

        Attendance count = new Attendance();
        count.setName("姓名");
        count.setDate("性别");
        count.setSex("工时合计");
        count.setHours("加班合计");
        count.setAddHours("加班工资合计");
        count.setAddPrice("总工资合计");
        count.setAllPrice("预支合计");
        count.setEatPrice("剩余未结");
        list.add(count);
        for (String name : nameGroup.keySet()) {
            list.add(nameGroup.get(name));
        }

        Result result = new Result();
        if (null != list && list.size() > 0) {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            result.setCode(1);
            // 数据下载
            try {
                EasyExcel.write(response.getOutputStream(), Attendance.class)
                        .excelType(ExcelTypeEnum.XLSX)
                        .inMemory(true)
                        .sheet("考勤表")
                        .doWrite(list);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            result.setCode(0);
            result.setMessage("未查询到相关数据！");
        }
    }

    private String format2(double data) {
        DecimalFormat f = new DecimalFormat("0.00");
        return f.format(data);
    }
}
